package com.software.levelupandroid.utils;

import android.os.Build;

import androidx.annotation.RequiresApi;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @Description 日期工具类
 * @Author LiangYaJie
 * @CreateTime 2024年05月03日 20:30:05
 */
public class DateUtil {

    /**
     * 将LocalDateTime类型的数据转化为String类型的数据
     * @param dateTime
     * @param pattern
     * @return
     */
    public static String convertLocalDateTimeToString(LocalDateTime dateTime, String pattern){
        String str = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            str = dateTime.format(formatter);
        }
        return str;
    }

    /**
     * 将Long类型的时间转化为LocalDateTime类型时间
     * @param aLong
     * @return
     */
    public static LocalDateTime convertLongToLocalDateTime(Long aLong){
        if (aLong == null){
            throw new IllegalArgumentException("Timestamp cannot be null");
        }

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Instant instant = Instant.ofEpochMilli(aLong);
            //获取用户手机的默认时区
            TimeZone tz = TimeZone.getDefault();
            String timezoneId = tz.getID();
            ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of(timezoneId));

            return zonedDateTime.toLocalDateTime();
        }else {
            throw new UnsupportedOperationException("LocalDateTime is not supported on this Android version");
        }

    }

    /**
     * 将Long类型的日期转化为String类型的日期，并转化为指定的格式
     * @param dateSecond
     * @param pattern
     * @return
     */
    public static String convertDateSecondToString(Long dateSecond, String pattern){
        //设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.getDefault());
        //将Long对象转化为date对象
        Date date = new Date(dateSecond);
        //将date对象转化为指定格式的String类型的日期
        String time = sdf.format(date);
        return time;
    }

    /**
     * 将String类型的日期转化为LocalDateTime类型的数据
     * @param date
     * @param pattern
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static LocalDateTime convertStringToLocalDateTime(String date, String pattern){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
        return dateTime;
    }

    /**
     * 将String类型的日期转化为Long类型的日期
     * @param date
     * @param pattern
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static Long convertStringToLong(String date, String pattern){
        LocalDateTime dateTime = convertStringToLocalDateTime(date, pattern);
        ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
        long timestamp = zonedDateTime.toInstant().toEpochMilli();
        return timestamp;
    }

    /**
     * 将Long类型的日期转化为String类型的日期
     * @param aLong
     * @param pattern
     * @return
     */
    public static String convertLongToString(Long aLong, String pattern){
        LocalDateTime dateTime = convertLongToLocalDateTime(aLong);
        return convertLocalDateTimeToString(dateTime,pattern);
    }

    /**
     * 将LocalDateTime类型的数据转化为Long类型的数据
     * @param dateTime
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static Long convertLocalDateTimeToLong(LocalDateTime dateTime){
        ZoneId zoneId = ZoneId.systemDefault(); //获取默认时区

        // 将LocalDateTime与时区结合，转换为ZonedDateTime
        ZonedDateTime zonedDateTime = dateTime.atZone(zoneId);

        // 将ZonedDateTime转换为Instant（代表从UNIX纪元开始的时间点）
        Instant instant = zonedDateTime.toInstant();

        // 将Instant转换为自UNIX纪元以来的毫秒数
        long epochMilli = instant.toEpochMilli();

        return epochMilli;
    }

    /**
     * 获取当前字符串类型的时间
     * @return
     */
    public static String getNowTime(){
        String nowTime = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm");
            nowTime = formatter.format(now);
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm", Locale.getDefault());
            nowTime = sdf.format(new Date());
        }

        return nowTime;
    }

    /**
     * 获取指定类型的当前时间
     * @param pattern
     * @return
     */
    public static String getNowDate(String pattern){
        String nowTime = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            nowTime = formatter.format(now);
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.getDefault());
            nowTime = sdf.format(new Date());
        }

        return nowTime;
    }

    /**
     * 获取当前Long类型的时间
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static Long getNowTimeLong(){
        LocalDateTime now = LocalDateTime.now();
        return convertLocalDateTimeToLong(now);
    }

    /**
     * 计算时间差（以分钟为单位）
     * @param time1
     * @param time2
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static long getTimeDuration(LocalDateTime time1, LocalDateTime time2){
        ZonedDateTime zonedDateTime1 = time1.atZone(ZoneId.systemDefault());
        ZonedDateTime zonedDateTime2 = time2.atZone(ZoneId.systemDefault());

        Instant instant1 = zonedDateTime1.toInstant();
        Instant instant2 = zonedDateTime2.toInstant();

        Duration duration = Duration.between(instant1, instant2);

        long minutes = duration.toMinutes();

        return minutes;
    }


    /**
     * 计算时间差（以毫秒显示）
     * @param time1
     * @param time2
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static long getLongDuration(String time1, String time2){

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm");

        LocalDateTime dateTime1 = LocalDateTime.parse(time1, formatter);
        LocalDateTime dateTime2 = LocalDateTime.parse(time2, formatter);

        Duration duration = Duration.between(dateTime1, dateTime2);

        long mills = duration.toMillis();

        return mills;
    }

    /**
     * 获取时间差，以分钟返回
     * @param startTime
     * @param endTime
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static long getMinutesDuration(String startTime, String endTime){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm");

        LocalDateTime start = LocalDateTime.parse(startTime, formatter);
        LocalDateTime end = LocalDateTime.parse(endTime, formatter);

        Duration duration = Duration.between(start, end);

        //将Duration转化为分钟
        long minutes = duration.toMinutes();

        return minutes;
    }


}
